﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Configuration;

namespace NoteSystem.Utils
{
    public class DbHelper
    {
        ///<summary>
        ///查询数据库，并返回一个数据容器
        ///</summary>
        ///<return></return>
        public static DataSet Query(string sql)
        {
            //获取数据库中的数据
            //1.创建数据库连接 参数：数据库地址
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            //2.创建适配器 参数1：sql语句 参数2：数据连接管道
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, connection);
            //3.创建数据库容器
            DataSet ds = new DataSet();
            //4.填充容器
            sqlDataAdapter.Fill(ds);
            return ds;

        }
        /// <summary>
        /// 执行操作语句并且返回受影响行数
        /// </summary>
        /// <param name="sql">操作语句</param>
        /// <returns></returns>
        public static int ExecuteSql(string sql)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlCommand cmd = new SqlCommand(sql, connection);
            connection.Open();
            int flag = cmd.ExecuteNonQuery();
            connection.Close();
            return flag;

        }
    }
}